// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function toggleOptionList(name) {
    $(name).toggleClassName('open');
}
function hideOptionList(name) {
    $(name).removeClassName('open');
}

function popupWindow(url, width, height) {
    window.open(url, '', 'height='+ height +',width=' + width + ',status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
}

function switchSearchBox(section) {
    $('search').className = 'on-' + section;
    return false;
}

function AjaxPostOnSubmit(form, successCallback, failureCallback) {
    new Ajax.Request(form.action, {
        method: 'post',
        parameters: form.serialize(true),
        onSuccess: successCallback,
        onFailure: failureCallback
    });
    return false;
}

function clear_rating_form(){
    document.getElementById('title').value = '';
    document.getElementById('rating').value = 5;
    document.getElementById('rating').checked = true;
    document.getElementById('note').value = '';
    return false;
}

// blurs tabs to fix IE CSS image loading issue
function change_tab(class_name, tab, tab_wrapper) {
    $(tab_wrapper).className = class_name;
    tab.blur();
    return false;
}

function switchSearchBox(current_tab){
    $('search').className = "on-" + current_tab;
    return false;
}


// reporting functions -- may move to a new js file if there are enough of them
function log_click(img_src) {
    // add an image to the images array here.
    var r = Math.floor(Math.random() * 1000);
    img_src += '&cb=' + r;
    log_image = new Image();
    log_image.src = img_src;
}

var link_types = new Object();
link_types.edit_note             = 21;
link_types.get_directions        = 14;
link_types.map_it                = 13;
link_types.save_business_listing = 11;
link_types.search_nearby         = 12;
link_types.send_to_mobile        = 33;

function log_tool_click(form, cmd) {
    var action = form.listing_tool.value;
    var link_type = link_types[action];
    if (link_type) {
        cmd = cmd.replace(/(lt=)(\d+)/, "$1" + link_type);
        log_click(cmd);
    }
}

function convertToUrlFriendly(string){
    var newString;
    newString = string.replace(/\s/g,'-');
    newString = newString.replace(/['"#]+/,'');
    return newString;
}

function moreInfoSwitch(aTag) {
    var tempArray = aTag.id.split('-');
    tempArray.pop();
    var sectionId = tempArray.join('-');
    var section = $(sectionId);
    if (!section.visible()) {
        // Handle hiding the sections, and displaying the newly selected section.
        var sections = document.getElementsByClassName('more-info-section');
        sections.each(function(s, index) {
            if (s.visible()) {
                new Effect.Fade(s.id, {duration:.5, fps:100});
            }
        });
        new Effect.Appear(sectionId, {duration:.5, fps:100, queue: 'end'});

        // Handle the active state of the more info links, make the new link active.
        var links = document.getElementsByClassName('more-info-links');
        var indexOfOnLink = null;
        links.each(function(s, index) {
            if (aTag.parentNode == s) {
                indexOfOnLink = index;
                s.addClassName('on');
            } else {
                s.removeClassName('on');
            }
        });
    }
    return false;
}

function switchRefinements(select) {
    var refinementGroups = $('refine-options').getElementsByClassName('options-menu');
    for (var i = 0; i < refinementGroups.length; i++) {
        refinementGroups[i].hide();
    }
    $(select.value).show();
    $('refine-options').blur();
}